CREATE PROCEDURE coreCmsMoveParent
(
	@cid			int,
	@newparent		int
)
AS
BEGIN
	SET NOCOUNT ON;

	/* Declarations */
	DECLARE		@originalrank	int;
	DECLARE		@originalparent	int;
	DECLARE		@newRank		int;

	/* Load info */
	SELECT	@originalrank = ISNULL(Rank,-1),
			@originalparent = ISNULL(FK_ParentID, -1)
	FROM	Content WHERE PK_ID=@cid;
	IF @originalrank = -1 RETURN;

	/* Re-order siblings from the original position */
	UPDATE Content SET Rank=Rank-1 WHERE FK_ParentID=@originalparent AND Rank>@originalrank;

	/* Get the next available rank at the new parent */
	SELECT @newRank = (ISNULL(MAX(Rank),0))+1 FROM Content WHERE FK_ParentID=@newparent;

	/* Move the record parent and set new rank */
	UPDATE Content SET 
		Rank			= @newRank, 
		FK_ParentID		= @newparent
	WHERE PK_ID=@cid;

	SET NOCOUNT OFF;	
END